<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('年月')" prop="years">
            <el-date-picker
              v-model="form.years"
              clearable
              style="width: 200px"
              type="month"
              value-format="yyyy-MM"
              :placeholder="$t('年月')"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('应收租金(元)')" prop="receivableRent">
            <Money v-model="form.receivableRent" :placeholder="$t('应收租金(元)')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实收租金(元)')" prop="paidInRent">
            <Money v-model="form.paidInRent" :placeholder="$t('实收租金(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实收日期')" prop="paidInDate">
            <el-date-picker
              v-model="form.paidInDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('实收日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('状态')" prop="rentState">
            <dict-select
              v-model="form.rentState"
              clearable
              dictionary="rent_state"
              :placeholder="$t('状态')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:rent" />
    </div>
  </div>
</template>

<script>
    import { saveDealLeaseRent, getDealLeaseRent } from '@/api/deal/immovable/dealLease'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  paidInRent: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  paidInDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                }
            }
        },
      watch: {
        'form.paidInRent': {
          handler() {
            if (this.form.paidInRent) {
              this.form.rentState = 'YJ'
            } else {
              this.form.rentState = 'WJ'
            }
          }
        }
      },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                  getDealLeaseRent(data.id).then(response => {
                        this.form = response.data
                        // if (!this.form.paidInRent) {
                        //   this.form.paidInRent = this.form.receivableRent
                        // }
                        // if (!this.form.paidInDate) {
                        //   this.form.paidInDate = new Date()
                        // }
                    // if (this.form.paidInRent) {
                    //   this.form.rentState = 'YJ'
                    // } else {
                    //   this.form.rentState = 'WJ'
                    // }
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    leaseId: undefined,
                    years: undefined,
                    receivableRent: undefined,
                    paidInRent: undefined,
                    paidInDate: undefined,
                    rentState: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen2 = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen2 !== undefined) {
                  this.$parent.$parent.formOpen2 = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                      saveDealLeaseRent(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          isRentState(row) {
              if (row) {
             this.form.rentState = 'YJ'
              } else {
                this.form.rentState = 'WJ'
              }
          }
        }
    }
</script>
